using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting.WindowsServices;
using Microsoft.Extensions.Hosting;
using MQTTnet.AspNetCore;
using System.Diagnostics;
using System.IO;

namespace MQTT.Broker
{
    public class Program
    {
        public static void Main(string[] args)
        {
#if DEBUG
           CreateHostBuilder(args).Build().Run();
#else
            var pathToExe = Process.GetCurrentProcess().MainModule.FileName;
            var pathToContentRoot = Path.GetDirectoryName(pathToExe);
            var host = WebHost.CreateDefaultBuilder(args).UseContentRoot(pathToContentRoot).UseStartup<Startup>().UseKestrel(
                o =>
                {
                    o.ListenAnyIP(Config.WsPort);
                    o.ListenAnyIP(Config.TcpPort, t => t.UseMqtt());
                }
                
                ).Build();
            host.RunAsService();
#endif
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                                .ConfigureWebHostDefaults(webBuilder =>
                                {
                                    webBuilder.UseStartup<Startup>().UseKestrel(o =>
                                    {
                                        o.ListenAnyIP(Config.WsPort);
                                        o.ListenAnyIP(Config.TcpPort, t => t.UseMqtt());
                                    });
                                });
    }
}
